package com.bootdo.wx.controller;

import com.bootdo.wx.domain.GoodsClassDO;
import com.bootdo.wx.service.GoodsClassService;
import com.bootdo.common.config.Constant;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.bootdo.common.utils.ShiroUtils.getUserId;

/**
 * 商品分类
 * 
 * @author sys
 * @email 1992lcg@163.com
 * @date 2019-03-19 16:39:22
 */
 
@Controller
@RequestMapping("/wx/goodsClass")
public class GoodsClassController {
	@Autowired
	private GoodsClassService goodsClassService;
	
	@GetMapping()
	@RequiresPermissions("wx:goodsClass:goodsClass")
	String GoodsClass(){
	    return "wx/goodsClass/goodsClass";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:goodsClass:goodsClass")
	public List<GoodsClassDO> list(@RequestParam Map<String, Object> params){
		//查询列表数据
		List<GoodsClassDO> goodsClassList = goodsClassService.list(params);
		return goodsClassList;
	}
	
	@GetMapping("/add/{pId}")
	@RequiresPermissions("wx:goodsClass:add")
	String add(Model model, @PathVariable("pId") String pId){
		model.addAttribute("pId", pId);
		if ("0".equals(pId)) {
			model.addAttribute("pName", "根目录");
			model.addAttribute("mineLevel", 0);
		} else {
			GoodsClassDO goodsClass = goodsClassService.get(pId);
			model.addAttribute("mineLevel", goodsClass.getLevel()+1);
			model.addAttribute("pName", goodsClass.getName());
			if(!"0".equals(goodsClass.getParentId())){
				return "error/smile.html";
			}
		}
		
	    return "wx/goodsClass/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:goodsClass:edit")
	String edit(@PathVariable("id") String id,Model model){
		GoodsClassDO goodsClass = goodsClassService.get(id);
		String pId = goodsClass.getParentId();
		model.addAttribute("pId", pId);
		if ("0".equals(pId)) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName", goodsClassService.get(pId).getName());
		}
		model.addAttribute("mineLevel", goodsClass.getLevel());
		model.addAttribute("goodsClass", goodsClass);
	    return "wx/goodsClass/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:goodsClass:add")
	public R save( GoodsClassDO goodsClass){
		goodsClass.setId(UuidUtil.get32UUID());
		goodsClass.setGmtCreate(new Date());
		goodsClass.setDelFlag(Constant.DEFAULT_DEL_FLAG);
		goodsClass.setUserIdCeate(getUserId());

		//取父级ID 和 本身类型
		String parentId = goodsClass.getParentId();
		Integer level = goodsClass.getLevel();
		if ("0".equals(parentId)){
			if (level==1 ){
				return R.error("顶级目录不能直接创建小类");
			}

		}else {
			if (0==level){
				return R.error("小类下不能创建大类");
			}
			//检测二级目录
			GoodsClassDO parentClass = goodsClassService.get(parentId);
			if (!"0".equals(parentClass.getParentId())){
				return R.error("最多创建二级级分类");
			}
		}

		if(goodsClassService.save(goodsClass)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:goodsClass:edit")
	public R update( GoodsClassDO goodsClass){
		//取父级ID 和 本身类型
		String parentId = goodsClass.getParentId();
		Integer level = goodsClass.getLevel();
		if ("0".equals(parentId)){
			if (1==level ){
				return R.error("顶级目录不能直接创建小类");
			}

		}else {
			if (0==level){
				return R.error("小类下不能创建大类");
			}
			//检测二级目录
			GoodsClassDO parentClass = goodsClassService.get(parentId);
			if (!"0".equals(parentClass.getParentId())){
				return R.error("最多创建二级分类");
			}
		}

		goodsClass.setGmtModified(new Date());
		goodsClassService.update(goodsClass);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:goodsClass:remove")
	public R remove( String id){
		GoodsClassDO goodsClassDO = goodsClassService.get(id);
		goodsClassDO.setDelFlag(1);
		goodsClassDO.setGmtModified(new Date());
		if(goodsClassService.update(goodsClassDO)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:goodsClass:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		for (String id : ids) {
			GoodsClassDO goodsClassDO = goodsClassService.get(id);
			goodsClassDO.setDelFlag(1);
			goodsClassDO.setGmtModified(new Date());
			goodsClassService.update(goodsClassDO);
		}
		return R.ok();
	}

	/**
	 * 分类选择树
	 *
	 * @param id
	 * @return
	 */
	@ResponseBody
	@GetMapping("/tree")
	public List<Map<String, Object>> getClassTree() {
		List<GoodsClassDO> list = goodsClassService.queryAll();
		List<Map<String, Object>> tree = new ArrayList<>();
		for (GoodsClassDO menu : list) {
			Map<String, Object> obj = new HashMap<>();
			obj.put("id", menu.getId());
			obj.put("text", menu.getName());
			obj.put("children", 0);
			tree.add(obj);
		}
		return tree;
	}


	@GetMapping("/treeT")
	@ResponseBody
	Tree<GoodsClassDO> tree() {
		Tree<GoodsClassDO>  tree = goodsClassService.getTree();
		return tree;
	}
	
}
